package com.youzhi.backend.controller;


import com.youzhi.backend.dto.Result;
import com.youzhi.backend.entity.Organization;
import com.youzhi.backend.request.OrganizationEditRequest;
import com.youzhi.backend.request.OrganizationListRequest;
import com.youzhi.backend.service.IOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门组织表 前端控制器
 * </p>
 *
 * @author xumeng
 * @since 2022-12-10
 */
@RestController
@Api(tags = "组织架构")
@RequestMapping("/organization")
public class OrganizationController extends BaseController {


    @Autowired
    private IOrganizationService service;


    @ApiOperation("组织架构列表")
    @PostMapping("/list")
    public Result list(@RequestBody OrganizationListRequest request) {
        verifyIfBlank(request, Organization.Fields.parentId);
        List<Map<String, Object>> organizations = service.listByParentId(request.getParentId());
        return Result.ok(organizations);
    }


    @ApiOperation("修改组织架构")
    @PostMapping("/update")
    public Result update(@RequestBody OrganizationEditRequest request) {
        verifyIfBlank(request, OrganizationEditRequest.Fields.id);
        service.updateOrganization(request);
        return Result.ok();
    }


    @ApiOperation("新增组织架构")
    @PostMapping("/add")
    public Result add(@RequestBody OrganizationEditRequest request) {
        verifyIfBlank(request, OrganizationEditRequest.Fields.parentId,
                OrganizationEditRequest.Fields.name,
                OrganizationEditRequest.Fields.description);
        service.addOrganization(request);
        return Result.ok();
    }


    @ApiOperation("删除组织架构")
    @PostMapping("/delete")
    public Result delete(@RequestBody OrganizationEditRequest request) {
        verifyIfBlank(request, OrganizationEditRequest.Fields.id);
        service.deleteOrganization(request.getId());
        return Result.ok();
    }


    @ApiOperation("列出所有组织架构")
    @PostMapping("/list/all")
    public Result listAll() {
        List<Organization> list = service.list();
        return Result.ok(list);
    }


    @ApiOperation("指定组织架构负责人")
    @PostMapping("/assign/principle")
    public Result assignPrincipleId(@RequestBody OrganizationEditRequest request) {
        verifyIfBlank(request, OrganizationEditRequest.Fields.id,
                OrganizationEditRequest.Fields.principleId);
        service.assignPrincipleId(request.getId(),request.getPrincipleId());
        return Result.ok();
    }

}

